{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0c59dd54-89b1-4e04-b802-08afbcc4da30",
   "metadata": {},
   "outputs": [],
   "source": [
    "class ReferenceFrame:\n",
    "    \"\"\"\n",
    "    说明：\n",
    "    这里是按照deepseek原本设计的程序，在此基础上看看还需要怎么修改\n",
    "    总的来说……deepseek确实能写程序，但感觉“优美性”还差一点……\n",
    "    物理很多变量相互关联，在此基础上一步一步探索吧？\n",
    "    不过总之还是先按照deepseek初始“一维”的框架入手吧\n",
    "\n",
    "    要实现的变量与功能：\n",
    "    速度，从而算出beta与gamma\n",
    "    从未知的方式得出速度\n",
    "\n",
    "    吐槽：\n",
    "    godot的功能与python真挺像的，几乎完美过渡了\n",
    "\n",
    "    现在先以简单的方式来写吧\n",
    "    \"\"\"\n",
    "    \n",
    "    def __init__(self, velocity=0):\n",
    "        self._velocity = velocity  # 速度，以光速 c 为单位（无量纲）\n",
    "        self.beta = v #光速c为单位，所以是一致的（本来是v/c）\n",
    "        self._velocity_init(velocity)\n",
    "\n",
    "\n",
    "    def _velocity_init(self,v):\n",
    "    #根据速度直接算出其他的量\n",
    "        if abs(self.v) >= 1: # 参考系还是不要直接到光速的好，不然无限了\n",
    "            raise ValueError(\"速度绝对值必须小于光速 (|v| < 1)\")\n",
    "            self.gamma = infinity\n",
    "            self.rapidity = infinity if self.v > 0 else -infinity\n",
    "        else:\n",
    "            self.gamma = 1 / sqrt(1 - self.v**2) # 洛伦兹因子\n",
    "            self.rapidity = arctanh(self.v)\n",
    "\n",
    "\n",
    "    def relative_velocity(self, other):\n",
    "        \"\"\"计算另一参考系相对于当前参考系的相对速度\"\"\"\n",
    "        return (other.v - self.v) / (1 - self.v * other.v)\n",
    "\n",
    "    def lorentz_transform(self, t, x, other):\n",
    "        \"\"\"将事件 (t, x) 从当前参考系变换到另一参考系，返回 (t', x')\"\"\"\n",
    "        u = self.relative_velocity(other)\n",
    "        gamma = 1 / sqrt(1 - u**2) if u != 1 else infinity\n",
    "        t_prime = gamma * (t - u * x)\n",
    "        x_prime = gamma * (x - u * t)\n",
    "        return (t_prime, x_prime)\n",
    "\n",
    "    def time_dilation(self, proper_time, other):\n",
    "        \"\"\"计算原时 (当前系) 在另一参考系中的时间膨胀\"\"\"\n",
    "        u = abs(self.relative_velocity(other))  # 速度取绝对值\n",
    "        gamma = 1 / sqrt(1 - u**2) if u != 1 else infinity\n",
    "        return gamma * proper_time\n",
    "\n",
    "    def length_contraction(self, proper_length, other):\n",
    "        \"\"\"计算原长 (当前系) 在另一参考系中的长度收缩\"\"\"\n",
    "        u = abs(self.relative_velocity(other))  # 速度取绝对值\n",
    "        gamma = 1 / sqrt(1 - u**2) if u != 1 else infinity\n",
    "        return proper_length / gamma\n",
    "\n",
    "    def velocity_addition(self, w, other):\n",
    "        \"\"\"将当前参考系中的速度 w 转换为另一参考系中的速度\"\"\"\n",
    "        u = self.relative_velocity(other)\n",
    "        return (w - u) / (1 - w * u)\n",
    "        # 纵向速度叠加，书上19页\n",
    "        # 另外在20页还有横向的……横向这边的推导有点离谱（太复杂了）\n",
    "\n",
    "    ################\n",
    "    #出了点问题……\n",
    "    \n",
    "\n",
    "    #super(ReferenceFrame)\n",
    "\n",
    "    def relativistic_mass(self, rest_mass):\n",
    "        \"\"\"计算相对论质量 m = γm₀\"\"\"\n",
    "        return self.gamma * rest_mass\n",
    "\n",
    "    def momentum(self, rest_mass):\n",
    "        \"\"\"计算动量 p = γm₀v\"\"\"\n",
    "        return self.gamma * rest_mass * self.v\n",
    "\n",
    "    def add_rapidity(self, delta_w):\n",
    "        \"\"\"通过快度叠加生成新参考系（w_total = w + delta_w）\"\"\"\n",
    "        new_w = self.rapidity + delta_w\n",
    "        new_v = tanh(new_w)\n",
    "        return ReferenceFrame(new_v)\n",
    "\n",
    "    def transform_momentum(self, energy, momentum, other):\n",
    "        \"\"\"将能量和动量从当前参考系变换到另一参考系（一维情况）\"\"\"\n",
    "        u = self.relative_velocity(other)\n",
    "        gamma_u = 1 / sqrt(1 - u**2)\n",
    "        # 洛伦兹变换矩阵（简化为速度沿x轴）\n",
    "        energy_prime = gamma_u * (energy - u * momentum)\n",
    "        momentum_prime = gamma_u * (momentum - u * energy)\n",
    "        return (energy_prime, momentum_prime)\n",
    "\n",
    "    # 保留原有方法（如 relative_velocity, lorentz_transform 等）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cfc5764-8842-41fc-a3a1-6a03c5d49477",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 10.5",
   "language": "sage",
   "name": "sagemath"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
